<?php
class phpinchains_psp_PSPPage {

/**
 * Returns an instance of the handler of the specified tag
 *
 * @param string $namespace Namespace name
 * @param string $name      Tag name
 * @return object Tag class instance
 */
    function & getTagInstance($namespace, $name) {
        $classname = $name.'Handler';
        if (!class_exists($namespace."_".$name)) {
            $path = $this->basedir.'tags/';
            if (file_exists($path.$namespace."/".$classname.'.php')) {
                require_once($path.$namespace."/".$classname.'.php');
            }
            else {
                m2import('phpinchains/tags/'.$namespace."/".$classname.'.php');
            }
        }
        $classname = $namespace."_".$classname;
        $obj = new $classname;
        return $obj;
    }

    /**
     *
     * @param string $formname  Name of the form to check
     * @param string $type      Name of the form to check
     * @param string $fieldname Name of the field to check validity of
     */
    function isValidSearchFormField($formname, $type, $fieldname) {
        if (isset($this->requestableDataCode) && isset($this->requestableDataCode['xt:searchForm_'.$formname]['types'][$type])) {
            if (is_array($this->requestableDataCode['xt:searchForm_'.$formname]['types'][$type]['fields'])) {
                foreach ($this->requestableDataCode['xt:searchForm_'.$formname]['types'][$type]['fields'] as $field) {
                    if ($field['name'] == $fieldname) return true;
                }
            }
        }
        return false;
    }

    function getFormFields($form) {
        return $this->fieldMap[$form];
    }

    function getFileFormFields($form) {
        return $this->fileFieldMap[$form];
    }

    function validateField(& $env, & $actionform, $fieldname, $validationParam, $validatormapping) {
        $vals = array ();
        if (isset ($this->validatormap[$validatormapping]['class']))
            $vals[] = $this->validatormap[$validatormapping];
        else
            $vals = $this->validatormap[$validatormapping];
        if (!is_array($vals)) {
            print "<strong>No mapping for validator '$validatormapping' in action-config.xml! Remember: the tag conf:validators initializes the built-in validators.</strong><br/>";
            return;
        }
        foreach ($vals as $val) {
            if (!isset($val['param'])) $val['param'] = '';
            if (!isset($val['name'])) $val['name'] = '';
            if (!is_object($this->objcache[$val['class']])) {
                if (isset ($val['class'])) {
                    if (m2import($val['class'] . '.php', true))  // silent import
                    {
                        $classname = str_replace('/', '_', $val['class']);
                        $this->objcache[$val['class']] = new $classname ();
                    }
                    else {
                        print '<strong>Validator not found! The registered validator \'' . $val['class'] . '\' with mapping \'' . $validatormapping . '\'</strong>';
                    }
                }
                else {
                    print '<strong>Validator not found! Is validator \'' . $validatormapping . '\' registered in the action-config.xml of this module? (if conf:validators tag is missing in the config file than even the standard validators are not registered)</strong>';
                }
            }
            if ($this->objcache[$val['class']]-> {$val['method'] }
            ($env, $actionform, $fieldname, $val['param'] . ($val['param'] && $validationParam ? ';' : '') . $validationParam)) {
                $errors = & $actionform->getRef('validationErrors');
                $errors->addField($fieldname.'_'.$validatormapping);
            }
        }
    }

    function compilePSP($sfile, $cfile, $viewname) {
        $this->dispatcher->compilePSP($sfile, $cfile, $viewname);
    }

    function validate(& $dispatcher, & $env, & $actionform, $formname, $basedir) {

    }

    function render(& $dispatcher, & $env, & $actionform, $basedir) {

    }

}
?>